#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcolor.h"
#include "cinemasystem.h"
#include "userclass.h"
//#include "logindialog.h"
#include "selection.h"
#include "iostream"
#include <QTimer>
#include <QtDebug>

bool button_usage_cush[50];
bool button_locked[50];
bool button_usage[50];

int hall_number;

extern std::string username;
extern std::string pwd_2;

MainWindow::MainWindow(QWidget *parent,int number) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    CinemaSystem cinema("2004-11-22","1",100);
    hall_number = number;
    std::vector<CinemaSystem::Auditorium> halls = cinema.readAuditoriumsFromFile("D:\\QT_projects\\Login\\2.txt");
    CinemaSystem::Auditorium hall = halls[number];
    std::cout<<number<<std::endl;
    int i= 1 ;
    static int flag = 0;
    if(flag == 0){
    for(const auto&seat :hall.seats){

        if(seat.status == CinemaSystem::SeatStatus::SOLD){

            button_usage[i++] = true;
        }
        else {
            button_usage[i++] = false;

        }
    }

    for(int i=0;i<=50;i++){
        //button_usage_cush[i] = true;
        button_usage_cush[i] = button_usage[i];
    }

    flag++;
    }
    refresh();

}

MainWindow::~MainWindow()
{
    delete ui;
}

const int rows = 6;
const int cols = 4;

std::string convertSeatNumber(int num) {
    if (num < 1 || num > rows * cols) {
        return "Invalid seat number!";
    }

    // 计算行号和列号
    int row = (num - 1) / cols;
    int col = (num - 1) % cols;

    // 转换行号到字母
    char rowLetter = 'A' + row;

    // 返回座位编号
    return std::string(1, rowLetter) + std::to_string(col + 1);
}

void MainWindow::on_pushButton_25_clicked()
{
    for(int i=1;i<=25;i++){
        button_usage[i] = button_usage_cush[i];
    }
    std::ofstream outFile("D:\\QT_projects\\Login\\4.txt", std::ios::trunc);
       if (!outFile) {
           std::cerr << "Error opening file for writing: " << "D:\\QT_projects\\Login\\4.txt" << std::endl;
           return;
       }
       for(int i = 0;i <hall_number ; i++){
           outFile << '\n' ;
       }
       for (int i = 1; i <= 24; i++) {
           outFile << (button_usage[i] ? '1' : '0');
       }
       outFile.close();
    ui->pushButton_25->setText("保存成功！");
    ui->pushButton_25->setStyleSheet("background-color: green;");
    for(int i = 1;i<=24; i++  ){
        if (button_usage_cush[i]==true){
            std::string seatid = convertSeatNumber(i);
            std::cout<<hall_number<<std::endl;
            userclass use(username,pwd_2,hall_number,seatid);
        }
    }
}

void MainWindow::refresh(){
    for(int i=0;i<=50;i++){
        //button_usage_cush[i] = true;
        button_locked[i] = button_usage[i];
    }
    if(button_locked[1]){
        ui->pushButton->setText("已选");
        ui->pushButton->setStyleSheet("background-color: red;");
    }
    if(button_locked[2]){
        ui->pushButton_2->setText("已选");
        ui->pushButton_2->setStyleSheet("background-color: red;");
    }
    if(button_locked[3]){
        ui->pushButton_3->setText("已选");
        ui->pushButton_3->setStyleSheet("background-color: red;");
    }
    if(button_locked[4]){
        ui->pushButton_4->setText("已选");
        ui->pushButton_4->setStyleSheet("background-color: red;");
    }
    if(button_locked[5]){
        ui->pushButton_5->setText("已选");
        ui->pushButton_5->setStyleSheet("background-color: red;");
    }
    if(button_locked[6]){
        ui->pushButton_6->setText("已选");
        ui->pushButton_6->setStyleSheet("background-color: red;");
    }
    if(button_locked[7]){
        ui->pushButton_7->setText("已选");
        ui->pushButton_7->setStyleSheet("background-color: red;");
    }
    if(button_locked[8]){
        ui->pushButton_8->setText("已选");
        ui->pushButton_8->setStyleSheet("background-color: red;");
    }
    if(button_locked[9]){
        ui->pushButton_9->setText("已选");
        ui->pushButton_9->setStyleSheet("background-color: red;");
    }
    if(button_locked[10]){
        ui->pushButton_10->setText("已选");
        ui->pushButton_10->setStyleSheet("background-color: red;");
    }
    if(button_locked[11]){
        ui->pushButton_11->setText("已选");
        ui->pushButton_11->setStyleSheet("background-color: red;");
    }
    if(button_locked[12]){
        ui->pushButton_12->setText("已选");
        ui->pushButton_12->setStyleSheet("background-color: red;");
    }
    if(button_locked[13]){
        ui->pushButton_13->setText("已选");
        ui->pushButton_13->setStyleSheet("background-color: red;");
    }
    if(button_locked[14]){
        ui->pushButton_14->setText("已选");
        ui->pushButton_14->setStyleSheet("background-color: red;");
    }
    if(button_locked[15]){
        ui->pushButton_15->setText("已选");
        ui->pushButton_15->setStyleSheet("background-color: red;");
    }
    if(button_locked[16]){
        ui->pushButton_16->setText("已选");
        ui->pushButton_16->setStyleSheet("background-color: red;");
    }
    if(button_locked[17]){
        ui->pushButton_17->setText("已选");
        ui->pushButton_17->setStyleSheet("background-color: red;");
    }
    if(button_locked[18]){
        ui->pushButton_18->setText("已选");
        ui->pushButton_18->setStyleSheet("background-color: red;");
    }
    if(button_locked[19]){
        ui->pushButton_19->setText("已选");
        ui->pushButton_19->setStyleSheet("background-color: red;");
    }
    if(button_locked[20]){
        ui->pushButton_20->setText("已选");
        ui->pushButton_20->setStyleSheet("background-color: red;");
    }
    if(button_locked[21]){
        ui->pushButton_21->setText("已选");
        ui->pushButton_21->setStyleSheet("background-color: red;");
    }
    if(button_locked[22]){
        ui->pushButton_22->setText("已选");
        ui->pushButton_22->setStyleSheet("background-color: red;");
    }
    if(button_locked[23]){
        ui->pushButton_23->setText("已选");
        ui->pushButton_23->setStyleSheet("background-color: red;");
    }
    if(button_locked[24]){
        ui->pushButton_24->setText("已选");
        ui->pushButton_24->setStyleSheet("background-color: red;");
    }
}

void MainWindow::on_pushButton_clicked()
{
    if(button_locked[1] == 0){
    static int seat_selection_flag = 0;
    seat_selection_flag++;
    if(seat_selection_flag%2 == 1){
       ui->pushButton->setStyleSheet("background-color: green;");
       ui->pushButton->update();
       ui->pushButton->setText("已选");
       button_usage_cush[1]=true;
    }else{
        ui->pushButton->setStyleSheet("background-color: white;");
        ui->pushButton->update();
        ui->pushButton->setText("座位1");
        button_usage_cush[1]=false;
        }
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    if(button_locked[2] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_2->setStyleSheet("background-color: green;");
           ui->pushButton_2->update();
           ui->pushButton_2->setText("已选");
           button_usage_cush[2]=true;
        }else{
            ui->pushButton_2->setStyleSheet("background-color: white;");
            ui->pushButton_2->update();
            ui->pushButton_2->setText("座位2");
            button_usage_cush[2]=false;
            }
        }
}

void MainWindow::on_pushButton_3_clicked()
{
    if(button_locked[3] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_3->setStyleSheet("background-color: green;");
           ui->pushButton_3->update();
           ui->pushButton_3->setText("已选");
           button_usage_cush[3]=true;
        }else{
            ui->pushButton_3->setStyleSheet("background-color: white;");
            ui->pushButton_3->update();
            ui->pushButton_3->setText("座位3");
            button_usage_cush[3]=false;
            }
        }
}

void MainWindow::on_pushButton_4_clicked()
{
    if(button_locked[4] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_4->setStyleSheet("background-color: green;");
           ui->pushButton_4->update();
           ui->pushButton_4->setText("已选");
           button_usage_cush[4]=true;
        }else{
            ui->pushButton_4->setStyleSheet("background-color: white;");
            ui->pushButton_4->update();
            ui->pushButton_4->setText("座位4");
            button_usage_cush[4]=false;
            }
        }
}

void MainWindow::on_pushButton_5_clicked()
{
    if(button_locked[5] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_5->setStyleSheet("background-color: green;");
           ui->pushButton_5->update();
           ui->pushButton_5->setText("已选");
           button_usage_cush[5]=true;
        }else{
            ui->pushButton_5->setStyleSheet("background-color: white;");
            ui->pushButton_5->update();
            ui->pushButton_5->setText("座位5");
            button_usage_cush[5]=false;
            }
        }
}

void MainWindow::on_pushButton_6_clicked()
{
    if(button_locked[6] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_6->setStyleSheet("background-color: green;");
           ui->pushButton_6->update();
           ui->pushButton_6->setText("已选");
           button_usage_cush[6]=true;
        }else{
            ui->pushButton_6->setStyleSheet("background-color: white;");
            ui->pushButton_6->update();
            ui->pushButton_6->setText("座位6");
            button_usage_cush[6]=false;
            }
        }
}

void MainWindow::on_pushButton_7_clicked()
{
    if(button_locked[7] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_7->setStyleSheet("background-color: green;");
           ui->pushButton_7->update();
           ui->pushButton_7->setText("已选");
           button_usage_cush[7]=true;
        }else{
            ui->pushButton_7->setStyleSheet("background-color: white;");
            ui->pushButton_7->update();
            ui->pushButton_7->setText("座位1");
            button_usage_cush[7]=false;
            }
        }
}

void MainWindow::on_pushButton_8_clicked()
{
    if(button_locked[8] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_8->setStyleSheet("background-color: green;");
           ui->pushButton_8->update();
           ui->pushButton_8->setText("已选");
           button_usage_cush[8]=true;
        }else{
            ui->pushButton_8->setStyleSheet("background-color: white;");
            ui->pushButton_8->update();
            ui->pushButton_8->setText("座位2");
            button_usage_cush[8]=false;
            }
        }
}

void MainWindow::on_pushButton_9_clicked()
{
    if(button_locked[9] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_9->setStyleSheet("background-color: green;");
           ui->pushButton_9->update();
           ui->pushButton_9->setText("已选");
           button_usage_cush[9]=true;
        }else{
            ui->pushButton_9->setStyleSheet("background-color: white;");
            ui->pushButton_9->update();
            ui->pushButton_9->setText("座位3");
            button_usage_cush[9]=false;
            }
        }
}

void MainWindow::on_pushButton_10_clicked()
{
    if(button_locked[10] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_10->setStyleSheet("background-color: green;");
           ui->pushButton_10->update();
           ui->pushButton_10->setText("已选");
           button_usage_cush[10]=true;
        }else{
            ui->pushButton_10->setStyleSheet("background-color: white;");
            ui->pushButton_10->update();
            ui->pushButton_10->setText("座位4");
            button_usage_cush[10]=false;
            }
        }
}

void MainWindow::on_pushButton_11_clicked()
{
    if(button_locked[11] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_11->setStyleSheet("background-color: green;");
           ui->pushButton_11->update();
           ui->pushButton_11->setText("已选");
           button_usage_cush[11]=true;
        }else{
            ui->pushButton_11->setStyleSheet("background-color: white;");
            ui->pushButton_11->update();
            ui->pushButton_11->setText("座位5");
            button_usage_cush[11]=false;
            }
        }
}

void MainWindow::on_pushButton_12_clicked()
{
    if(button_locked[12] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_12->setStyleSheet("background-color: green;");
           ui->pushButton_12->update();
           ui->pushButton_12->setText("已选");
           button_usage_cush[12]=true;
        }else{
            ui->pushButton_12->setStyleSheet("background-color: white;");
            ui->pushButton_12->update();
            ui->pushButton_12->setText("座位6");
            button_usage_cush[12]=false;
            }
        }
}

void MainWindow::on_pushButton_26_clicked()
{
    for(int i=0;i<=50;i++){
        button_usage_cush[i] = 0;
    }

    ui->pushButton_25->setText("确认选择");
    ui->pushButton_25->setStyleSheet("background-color: white;");
}

void MainWindow::on_pushButton_13_clicked()
{
    if(button_locked[13] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_13->setStyleSheet("background-color: green;");
           ui->pushButton_13->update();
           ui->pushButton_13->setText("已选");
           button_usage_cush[13]=true;
        }else{
            ui->pushButton_13->setStyleSheet("background-color: white;");
            ui->pushButton_13->update();
            ui->pushButton_13->setText("座位1");
            button_usage_cush[13]=false;
            }
        }
}

void MainWindow::on_pushButton_14_clicked()
{
    if(button_locked[14] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_14->setStyleSheet("background-color: green;");
           ui->pushButton_14->update();
           ui->pushButton_14->setText("已选");
           button_usage_cush[14]=true;
        }else{
            ui->pushButton_14->setStyleSheet("background-color: white;");
            ui->pushButton_14->update();
            ui->pushButton_14->setText("座位2");
            button_usage_cush[14]=false;
            }
        }
}

void MainWindow::on_pushButton_15_clicked()
{
    if(button_locked[15] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_15->setStyleSheet("background-color: green;");
           ui->pushButton_15->update();
           ui->pushButton_15->setText("已选");
           button_usage_cush[15]=true;
        }else{
            ui->pushButton_15->setStyleSheet("background-color: white;");
            ui->pushButton_15->update();
            ui->pushButton_15->setText("座位3");
            button_usage_cush[15]=false;
            }
        }
}

void MainWindow::on_pushButton_16_clicked()
{
    if(button_locked[16] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_16->setStyleSheet("background-color: green;");
           ui->pushButton_16->update();
           ui->pushButton_16->setText("已选");
           button_usage_cush[16]=true;
        }else{
            ui->pushButton_16->setStyleSheet("background-color: white;");
            ui->pushButton_16->update();
            ui->pushButton_16->setText("座位4");
            button_usage_cush[16]=false;
            }
        }
}

void MainWindow::on_pushButton_17_clicked()
{
    if(button_locked[17] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_17->setStyleSheet("background-color: green;");
           ui->pushButton_17->update();
           ui->pushButton_17->setText("已选");
           button_usage_cush[17]=true;
        }else{
            ui->pushButton_17->setStyleSheet("background-color: white;");
            ui->pushButton_17->update();
            ui->pushButton_17->setText("座位5");
            button_usage_cush[17]=false;
            }
        }
}

void MainWindow::on_pushButton_18_clicked()
{
    if(button_locked[18] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_18->setStyleSheet("background-color: green;");
           ui->pushButton_18->update();
           ui->pushButton_18->setText("已选");
           button_usage_cush[18]=true;
        }else{
            ui->pushButton_18->setStyleSheet("background-color: white;");
            ui->pushButton_18->update();
            ui->pushButton_18->setText("座位6");
            button_usage_cush[18]=false;
            }
        }
}

void MainWindow::on_pushButton_19_clicked()
{
    if(button_locked[19] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_19->setStyleSheet("background-color: green;");
           ui->pushButton_19->update();
           ui->pushButton_19->setText("已选");
           button_usage_cush[19]=true;
        }else{
            ui->pushButton_19->setStyleSheet("background-color: white;");
            ui->pushButton_19->update();
            ui->pushButton_19->setText("座位1");
            button_usage_cush[19]=false;
            }
        }
}

void MainWindow::on_pushButton_20_clicked()
{
    if(button_locked[20] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_20->setStyleSheet("background-color: green;");
           ui->pushButton_20->update();
           ui->pushButton_20->setText("已选");
           button_usage_cush[20]=true;
        }else{
            ui->pushButton_20->setStyleSheet("background-color: white;");
            ui->pushButton_20->update();
            ui->pushButton_20->setText("座位2");
            button_usage_cush[20]=false;
            }
        }
}

void MainWindow::on_pushButton_21_clicked()
{
    if(button_locked[21] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_21->setStyleSheet("background-color: green;");
           ui->pushButton_21->update();
           ui->pushButton_21->setText("已选");
           button_usage_cush[21]=true;
        }else{
            ui->pushButton_21->setStyleSheet("background-color: white;");
            ui->pushButton_21->update();
            ui->pushButton_21->setText("座位3");
            button_usage_cush[21]=false;
            }
        }
}

void MainWindow::on_pushButton_22_clicked()
{
    if(button_locked[22] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_22->setStyleSheet("background-color: green;");
           ui->pushButton_22->update();
           ui->pushButton_22->setText("已选");
           button_usage_cush[22]=true;
        }else{
            ui->pushButton_22->setStyleSheet("background-color: white;");
            ui->pushButton_22->update();
            ui->pushButton_22->setText("座位4");
            button_usage_cush[22]=false;
            }
        }
}

void MainWindow::on_pushButton_23_clicked()
{
    if(button_locked[23] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_23->setStyleSheet("background-color: green;");
           ui->pushButton_23->update();
           ui->pushButton_23->setText("已选");
           button_usage_cush[23]=true;
        }else{
            ui->pushButton_23->setStyleSheet("background-color: white;");
            ui->pushButton_23->update();
            ui->pushButton_23->setText("座位5");
            button_usage_cush[23]=false;
            }
        }
}

void MainWindow::on_pushButton_24_clicked()
{
    if(button_locked[24] == 0){
        static int seat_selection_flag = 0;
        seat_selection_flag++;
        if(seat_selection_flag%2 == 1){
           ui->pushButton_24->setStyleSheet("background-color: green;");
           ui->pushButton_24->update();
           ui->pushButton_24->setText("已选");
           button_usage_cush[24]=true;
        }else{
            ui->pushButton_24->setStyleSheet("background-color: white;");
            ui->pushButton_24->update();
            ui->pushButton_24->setText("座位6");
            button_usage_cush[24]=false;
            }
        }
}
